using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Appearing : IState
{
    private FSM Manager;
    private Parameter parameter;

    public Appearing(FSM manager)
    {
        Manager = manager;
        parameter = manager.parameter;
    }

    public void OnEnter()
    {
        Manager.PlayAnimation(FSM_AniMationName.Appearing);
    }

    public void OnExit()
    {

    }

    public void OnUpdate()
    {
        if (Physics2D.OverlapCircle(parameter.Feet.position, 0.2f, parameter.layerGround))
        {
            Manager.TransitionState(StateType.Idle);
        }
    }
}

public class Idle : IState
{
    private FSM Manager;
    private Parameter parameter;

    public Idle(FSM manager)
    {
        Manager = manager;
        parameter = manager.parameter;
    }

    public void OnEnter()
    {
        parameter.JumpCount = 2;
        Manager.PlayAnimation(FSM_AniMationName.Idle);
    }

    public void OnExit()
    {

    }

    public void OnUpdate()
    {
        float runValue = Input.GetAxis("Horizontal");
        if (Mathf.Abs(runValue) > 0.01f)
        {
            Manager.TransitionState(StateType.Runing);
        }
        if (Input.GetButtonDown("Jump") && parameter.JumpCount == 2)
        {
            parameter.JumpCount--;
            Manager.TransitionState(StateType.Jump);
        }
    }
}
public class Running : IState
{
    private FSM Manager;
    private Parameter parameter;

    public Running(FSM manager)
    {
        Manager = manager;
        parameter = manager.parameter;
    }

    public void OnEnter()
    {
        Manager.PlayAnimation(FSM_AniMationName.Run);
    }

    public void OnExit()
    {

    }

    public void OnUpdate()
    {
        float runValue = Input.GetAxis("Horizontal");
        if (Mathf.Abs(runValue)>0.01f)
        {
            parameter.Player.velocity = new Vector2(runValue * parameter.RunSpeed, parameter.Player.velocity.y);
            parameter.playertrans.localRotation = Quaternion.Euler(0, runValue < 0 ? 180 : 0, 0);
        }
        else
        {
            Manager.TransitionState(StateType.Idle);
        }
        if (Input.GetButtonDown("Jump") && parameter.JumpCount == 2)
        {
            parameter.JumpCount--;
            Manager.TransitionState(StateType.Jump);
        }
        if (parameter.Player.velocity.y < 0&&!Physics2D.OverlapCircle(parameter.Feet.position, 0.2f, parameter.layerGround))
        {
            Manager.TransitionState(StateType.Falling);
        }
    }
}
public class Jumping : IState
{
    private FSM Manager;
    private Parameter parameter;

    public Jumping(FSM manager)
    {
        Manager = manager;
        parameter = manager.parameter;
    }

    public void OnEnter()
    {
        Manager.PlayAnimation(FSM_AniMationName.Jump);
        parameter.Player.velocity = Vector2.up * parameter.JumpSpeed;
    }

    public void OnExit()
    {

    }

    public void OnUpdate()
    {
        float runValue = Input.GetAxis("Horizontal");
        if (Mathf.Abs(runValue) > 0.01f)
        {
            parameter.Player.velocity = new Vector2(runValue * parameter.RunSpeed, parameter.Player.velocity.y);
            parameter.playertrans.localRotation = Quaternion.Euler(0, runValue < 0 ? 180 : 0, 0);
        }
        if (Input.GetButtonDown("Jump") && parameter.JumpCount == 1)
        {
            parameter.JumpCount--;
            Manager.TransitionState(StateType.DoubleJumping);
        }
        if (parameter.Player.velocity.y < 0)
        {
            Manager.TransitionState(StateType.Falling);
        }
    }
}
public class DoubleJumping  : IState
{
    private FSM Manager;
    private Parameter parameter;

    public DoubleJumping(FSM manager)
    {
        Manager = manager;
        parameter = manager.parameter;
    }

    public void OnEnter()
    {
        Manager.PlayAnimation(FSM_AniMationName.DoubleJump);
        parameter.Player.velocity = Vector2.up * parameter.DoubleJumpSpeed;
    }

    public void OnExit()
    {

    }

    public void OnUpdate()
    {
        float runValue = Input.GetAxis("Horizontal");
        if (Mathf.Abs(runValue) > 0.01f)
        {
            parameter.Player.velocity = new Vector2(runValue * parameter.RunSpeed, parameter.Player.velocity.y);
            parameter.playertrans.localRotation = Quaternion.Euler(0, runValue < 0 ? 180 : 0, 0);
        }
        if (parameter.Player.velocity.y < 0)
        {
            Manager.TransitionState(StateType.Falling);
        }
    }
}
public class Falling : IState
{
    private FSM Manager;
    private Parameter parameter;

    public Falling(FSM manager)
    {
        Manager = manager;
        parameter = manager.parameter;
    }

    public void OnEnter()
    {
        Manager.PlayAnimation(FSM_AniMationName.Fall);
    }

    public void OnExit()
    {

    }

    public void OnUpdate()
    {
        float runValue = Input.GetAxis("Horizontal");
        if (Mathf.Abs(runValue)>0.01f)
        {
            parameter.Player.velocity = new Vector2(runValue * parameter.RunSpeed, parameter.Player.velocity.y);
            parameter.playertrans.localRotation = Quaternion.Euler(0, runValue < 0 ? 180 : 0, 0);
        }
        if (Physics2D.OverlapCircle(parameter.Feet.position, 0.2f, parameter.layerGround))
        {
            Manager.TransitionState(StateType.Idle);
            parameter.JumpCount = 2;
        }
        if (Input.GetButtonDown("Jump") && parameter.JumpCount == 1)
        {
            parameter.JumpCount--;
            Manager.TransitionState(StateType.DoubleJumping);
        }
    }
}

public class Hurting : IState
{
    private FSM Manager;
    private Parameter parameter;

    public Hurting(FSM manager)
    {
        Manager = manager;
        parameter = manager.parameter;
    }

    public void OnEnter()
    {
        Manager.PlayAnimation(FSM_AniMationName.Hurt);
    }

    public void OnExit()
    {

    }

    public void OnUpdate()
    {
    }
}
